//
//  RMSecretController.m
//  RMNote
//
//  Created by lxf on 16/4/28.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMSecretController.h"
#import "TXTradePasswordView.h"


@interface RMSecretController ()<TXTradePasswordViewDelegate>{
    int _count;
}

@property (nonatomic, strong) NSString *passWorld;
@property (nonatomic, strong) TXTradePasswordView *txView;
@property (nonatomic, assign) BOOL isFirst;

@end

@implementation RMSecretController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [backBtn addTarget:self action:@selector(backbtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    [backBtn setTitle:@"取消" forState:(UIControlStateNormal)];
    [backBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    backBtn.frame = CGRectMake(10,30 , 40, 40);
    
    [self.view addSubview:backBtn];
    
    
    
   NSString *savePassword = [[NSUserDefaults standardUserDefaults]valueForKey:@"passWord"];
    if (!savePassword || savePassword.length==0) {
       self.txView = [[TXTradePasswordView alloc]initWithFrame:CGRectMake(0, 100, kScreenWidth, 120)WithTitle:@"请设置密码"];
        self.isFirst = YES;
        _count = 0;
    }else{
        self.txView = [[TXTradePasswordView alloc]initWithFrame:CGRectMake(0, 100, kScreenWidth, 120)WithTitle:@"请输入密码"];
        self.isFirst = NO;
    }
//    self.txView.backgroundColor = [UIColor orangeColor];
    [self.txView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(txViewTap)]];
    self.txView.TXTradePasswordDelegate = self;
    if (![self.txView.TF becomeFirstResponder]) {
        [self.txView.TF becomeFirstResponder];
    }
    [self.view addSubview:self.txView];
}

- (void)txViewTap{
    if (![self.txView.TF becomeFirstResponder]) {
        [self.txView.TF becomeFirstResponder];
    }
}

- (void)backbtnAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)TXTradePasswordView:(TXTradePasswordView *)view WithPasswordString:(NSString *)Password{
    if (self.isFirst) {
        if (_count == 0) {
            self.passWorld = nil;
            self.passWorld = Password;
            self.txView.lable_title.text = @"请再次输入";
            [self clearPoint];
            _count++;
        }else{
            if ([self.passWorld isEqualToString:Password]) {
                self.txView.lable_title.text = @"密码设置成功";
                [[NSUserDefaults standardUserDefaults]setValue:self.passWorld forKey:@"passWord"];
            }else{
                self.txView.lable_title.text = @"两次密码输入不一致,请重新输入";
                _count = 0;
                [self clearPoint];
            }
        }
    }else{
        if ([Password isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"passWord"]]) {
            self.passBlock();
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            self.txView.lable_title.text = @"密码输入错误请重新输入";
            [self clearPoint];
        }
    }

}

- (void)clearPoint{
    self.txView.TF.text = @"";
    self.txView.lable_point.hidden = YES;
    self.txView.lable_point2.hidden = YES;
    self.txView.lable_point3.hidden = YES;
    self.txView.lable_point4.hidden = YES;
    self.txView.lable_point5.hidden = YES;
    self.txView.lable_point6.hidden = YES;
}

#pragma mark  提示条
-(void)showMessage:(NSString *)message duration:(NSTimeInterval)time
{
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor grayColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:kRealValue(15)],
                                 NSParagraphStyleAttributeName:paragraphStyle.copy};
    
    CGSize labelSize = [message boundingRectWithSize:CGSizeMake(207, 999)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:attributes context:nil].size;
    
    label.frame = CGRectMake(10, 5, labelSize.width +20, labelSize.height);
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:kRealValue(15)];
    [showview addSubview:label];
    
    showview.frame = CGRectMake((screenSize.width - labelSize.width - 20)/2,
                                screenSize.height - 300,
                                labelSize.width+40,
                                labelSize.height+10);
    [UIView animateWithDuration:time animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}



@end
